find_program(XGETTEXT_EXECUTABLE xgettext)
find_program(MSGMERGE_EXECUTABLE msgmerge)
find_program(MSGFMT_EXECUTABLE msgfmt)

if(XGETTEXT_EXECUTABLE AND MSGMERGE_EXECUTABLE AND MSGFMT_EXECUTABLE)
	set(POT_FILE ${CMAKE_PROJECT_NAME}.pot)
	
	add_custom_command(
		OUTPUT ${POT_FILE}
		COMMAND ${XGETTEXT_EXECUTABLE}
		ARGS --output=${POT_FILE} --keyword=_ --keyword=N_ --directory=${CMAKE_SOURCE_DIR} --msgid-bugs-address=kindest87@gmail.com
		src/gsm-view.c src/gsm-watcher-source.c src/gsmon.c ui/gsmon.glade
		DEPENDS ../src/gsm-view.c ../src/gsm-watcher-source.c ../src/gsmon.c ../ui/gsmon.glade
		COMMENT "Generating PO template file"
	)

	set(I18N_MO_FILES)
	foreach(lang ${GSMON_LANGUAGES})
		set(PO_FILE ${lang}.po)
		set(MO_FILE ${lang}.mo)
		add_custom_command(
			OUTPUT ${MO_FILE}
			COMMAND ${MSGMERGE_EXECUTABLE} ARGS --update --quiet ${PO_FILE} ${POT_FILE}
			COMMAND ${MSGFMT_EXECUTABLE} ARGS --output ${MO_FILE} ${PO_FILE}
			DEPENDS ${POT_FILE} ${PO_FILE}
			COMMENT "Compiling locales (${lang})"
		)
		install(
			FILES ${MO_FILE}
			DESTINATION ${CMAKE_INSTALL_PREFIX}/share/locale/${lang}/LC_MESSAGES
			RENAME gsmon.mo
		)
		set(MO_FILES ${MO_FILES} ${MO_FILE})
	endforeach()

	add_custom_target(i18n ALL DEPENDS ${MO_FILES})
endif()
